package com.ftwj.controller.app;


import com.ftwj.dto.RegisterRequest;
import com.ftwj.entity.SysUser;
import com.ftwj.service.SysUserManageAppService;
import com.ftwj.utils.result.MessageEnum;
import com.ftwj.utils.result.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/app/user")
public class SysUserManageAppController {

    @Autowired
    private SysUserManageAppService sysUserManageAppService;

    @PostMapping("/app-register")
    public ResponseEntity<Map<String, Object>> register(
            @RequestBody RegisterRequest registerRequest) {
        Map<String, Object> response = new HashMap<>();
        try { // 添加缺失的 try 块
            // 注册用户
            SysUser user = registerRequest.getUser();
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            user.setState(0); // 设置用户状态为待审核
            sysUserManageAppService.register(user);
            response.put("status", "success");
            response.put("message", "注册成功，请等待审核");
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            log.error("注册失败: {}", e.getMessage(), e); // 添加日志记录
            response.put("status", "error");
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }


    @PostMapping("/app-login")
    public ResultBean login(@RequestParam String phoneNumber,
                            @RequestParam String password) {
        // 登录用户
        if (phoneNumber == null || phoneNumber.trim().isEmpty()) {
            return ResultBean.error(400, "手机号不能为空");
        }
        if (password == null || password.trim().isEmpty()) {
            return ResultBean.error(400, "密码不能为空");
        }
        return sysUserManageAppService.login(phoneNumber, password);
    }

}
